<template>
  <view>
    <!-- <my-search :bgcolor="'#c00000'"></my-search> -->
    <my-search @click="gotoSearch"></my-search> 
    <view class="scroll-view-container">
      <!-- 左侧的滚动视图区域 -->
      <scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
        <view class="left-scroll-view-item">
          <block v-for="(item, index) in cateList" :key="index">
            <view :class="['left-scroll-view-item', active === index ? 'active' : '']" @click="activeChange(index)">{{item.cat_name}}</view>
          </block>
        </view>
      </scroll-view>
      <!-- 右侧的滚动视图区域 -->
      <scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}" :scroll-top="scrollTop">
        <view class="cate-lv2" v-for="(item2, index2) in cateLevel2" :key="index2">
          <!-- 二级分类的标题 -->
          <view class="cate-lv2-title">/ {{item2.cat_name}} /</view>
          <!-- 三级分类列表 -->
          <view class="cate-lv3-list">
            <!-- 三级分类项 -->
            <view class="cate-lv3-item" v-for="(item3, index3) in item2.children" :key="index3" @click="gotoGoodsList(item3)"> 
              <image :src="item3.cat_icon" mode=""></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import mySearch from '../../components/my-search/my-search.vue';
  import badgeMix from '@/mixins/tabbar-badge.js';
  export default {   
    mixins: [badgeMix],
    data() {
      return {
        wh: 0 ,// 可以用vh解决窗口高度问题，也可以通过获取手机信息获得窗口高度
        // 使用100vh可能会造成左右两侧同时上下滚动的问题，使用后者更好
        cateList: [], // 分类数据列表
        active: 0, // 默认激活项
        scrollTop: 0,
        cateLevel2: [] // 二级分类列表
      };
    },
    onLoad(){
      const sysInfo = uni.getSystemInfoSync(); // 获取手机的相关信息，如名称、窗口长度等
      this.wh = sysInfo.windowHeight - 50; // -50是为了减去搜索栏的高度
      this.getCateList();
    },
    components: {
      "my-search": mySearch
    },
    methods: {
      async getCateList(){
        const {data: res} = await uni.$http.get('/api/public/v1/categories');
        if(res.meta.status !== 200) return uni.$showMsg();
        // console.log(res);
        this.cateList = res.message;
        this.cateLevel2 = this.cateList[this.active].children; // 通过cat_id获取二级列表，也可直接用数组索引
        // console.log(this.cateLevel2)
      },
      activeChange(index){
        this.active = index;
        this.scrollTop = this.scrollTop === 0 ? 0.1 : 0;
        this.cateLevel2 = this.cateList[index].children;
      },
      gotoGoodsList(item){
        uni.navigateTo({
          url: `/subpkg/goods_list/goods_list?cid=${item.cat_id}`
        });
      },
      gotoSearch(){
        uni.navigateTo({
          url: '/subpkg/search/search'
        });
      }
    }
  }
</script>

<style lang="scss">
.scroll-view-container{
  display: flex;
  .left-scroll-view{
    width: 240rpx;
    .left-scroll-view-item{
      background-color: #f7f7f7;
      line-height: 60px;
      text-align: center;
      font-size: 12px;
      &.active{
        background-color: #ffffff;
        position: relative;
        &::before{
          content: '';
          display: block;
          width: 3px;
          height: 30px;
          background-color: #c00000;
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }
  .right-scroll-view{
    background-color: #ffffff;
    .cate-lv2-title{
      font-size: 24rpx;
      font-weight: bold;
      text-align: center;
      padding: 30rpx 0;
    }
    .cate-lv3-list{
      display: flex;
      flex-wrap: wrap;
      .cate-lv3-item{
        width: 33.33%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        margin-bottom: 20rpx;
        image{
          width: 120rpx;
          height: 120rpx;
        }
        text{
          font-size: 24rpx;
        }
      }
    }
  }
}
</style>
